package com.springboot.service.tool.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.springboot.returndata.dto.tool.CardMessageDto;
import com.springboot.returndata.dto.tool.CreateImgDto;
import com.springboot.returndata.vo.tool.TestExcelVo;
import com.springboot.service.tool.ToolService;
import com.springboot.util.card.IdCardUtil;
import com.springboot.util.file.MyFileUtil;
import com.springboot.util.img.ImageUtil;
import com.springboot.util.pdf.ItextPdfUtils;
import com.springboot.util.uuid.DbUtilUuid;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Classname ToolService
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/4/12 11:42
 * @Created by zyj
 */
@Service
@Slf4j
public class ToolServiceImpl implements ToolService {

    @Resource
    private IdCardUtil idCardUtil;

    @Resource
    private ImageUtil imageUtil;

    @Resource
    private ItextPdfUtils itextPdfUtils;

    @Resource
    private DbUtilUuid dbUtilUuid;

    @Value("${spring.servlet.multipart.location}")
    private String locationPath;

    @Override
    public byte[] createIdCardFront(CardMessageDto cardMessage) {

		try {
            return idCardUtil.generateIdcodeZ( cardMessage);
        } catch (Exception e) {
			e.printStackTrace();
		}

        return null;
    }

    @Override
    public byte[] createIdCardBack(CardMessageDto cardMessage) {
        try {
            return idCardUtil.generateIdcodeF( cardMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public JSONArray analysisExcel(MultipartFile multipartFiles) throws IOException {
        String suffix = FileUtil.getSuffix(multipartFiles.getOriginalFilename());
        if(!(StrUtil.equals(suffix.toUpperCase(), "XLS") || StrUtil.equals(suffix.toUpperCase(), "XLSX"))){
            return null;
        }

        /**解析导入文件*/
        ExcelReader reader = ExcelUtil.getReader(multipartFiles.getInputStream(), 1);
        reader.addHeaderAlias("测试1", "cs1");
        reader.addHeaderAlias("测试2", "cs2");

        List<TestExcelVo> list = reader.read(0,1, TestExcelVo.class);

        return JSONUtil.parseArray(list);
    }

    @Override
    public byte[] createImg(CreateImgDto cardMessage) {
        String type = cardMessage.getType();
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            switch (type) {
                case "1" :
                    BufferedImage fy = imageUtil.generateCustomerSeal(cardMessage.getYz(), cardMessage.getFz());
                    ImageIO .write(fy, "png", byteArrayOutputStream);
                    return byteArrayOutputStream.toByteArray();
                case "2" :
                    BufferedImage fz = imageUtil.generatePrivateSeal( cardMessage.getFz());
                    ImageIO .write(fz, "png", byteArrayOutputStream);
                    return byteArrayOutputStream.toByteArray();

                case "3" :
                    BufferedImage yz = imageUtil.generatePrivateSeal( cardMessage.getYz());
                    ImageIO .write(yz, "png", byteArrayOutputStream);
                    return byteArrayOutputStream.toByteArray();
            }
        }catch (Exception e){
            log.error("createImg", e);
        }

        return new byte[0];
    }

    /**
     * 方法描述
     * //类型  1、替换关键字 2、根据关键字加图片 3、根据坐标添加图片
     *     private String type;
     *
     *     private String key;
     *
     *     private String value;
     *
     *     private String file;
     * @return: byte
     * @author: zyj
     * @date: 2024/5/13
     */
    @Override
    public byte[] createPdf(Map<String, String> params, MultipartFile file, MultipartFile img) throws Exception{

        //类型  1、替换关键字 2、根据关键字加图片 3、根据坐标添加图片
        String type = params.get("type");
        String key = params.get("key");
        String value = params.get("value");
        String x = params.get("x");
        String y = params.get("y");
        String fontSize = params.get("fontSize");

        switch (type){
            case "1":
                return itextPdfUtils.pdfReplaceByte(file.getBytes(), key, value, locationPath, file.getOriginalFilename()+dbUtilUuid.getDatabaseId(),
                        Integer.parseInt(x), Integer.parseInt(y), Float.parseFloat(fontSize));
            case "2":
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("KW", key);
                jsonObject.put("XOFFSET", x);
                jsonObject.put("YOFFSET", y);

                return itextPdfUtils.saveImgePdf(file.getBytes(), img.getBytes(), jsonObject);
            case "3":
                return itextPdfUtils.addImg(file.getBytes(), img.getInputStream(), img.getContentType(), Integer.parseInt(x), Integer.parseInt(y));
        }

        return new byte[0];
    }
}
